package demoMod.nanaki.patches.events.beyond;

import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractImageEvent;
import com.megacrit.cardcrawl.events.beyond.MoaiHead;
import com.megacrit.cardcrawl.helpers.ScreenShake;
import com.megacrit.cardcrawl.localization.EventStrings;
import com.megacrit.cardcrawl.vfx.cardManip.ShowCardAndObtainEffect;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.cards.tempCards.Reborn;
import demoMod.nanaki.characters.NanakiCharacter;

public class MoaiHeadPatch {
    public static final String ID = NanakiMod.makeID("The Moai Head");
    private static final EventStrings eventStrings = CardCrawlGame.languagePack.getEventString(ID);
    public static final String NAME = eventStrings.NAME;
    public static final String[] DESCRIPTIONS = eventStrings.DESCRIPTIONS;
    public static final String[] OPTIONS = eventStrings.OPTIONS;

    @SpirePatch(
            clz = MoaiHead.class,
            method = SpirePatch.CONSTRUCTOR
    )
    public static class PatchConstructor {
        public static void Postfix(MoaiHead event) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                int hpToLose = (int) Math.floor(AbstractDungeon.player.maxHealth * (AbstractDungeon.ascensionLevel >= 15 ? 0.18 : 0.125));
                event.imageEventText.updateDialogOption(0, String.format(OPTIONS[0], hpToLose), new Reborn());
            }
        }
    }

    @SpirePatch(
            clz = MoaiHead.class,
            method = "buttonEffect"
    )
    public static class PatchButtonEffect {
        public static SpireReturn<Void> Prefix(MoaiHead event, int buttonPressed) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                int screenNum = ReflectionHacks.getPrivate(event, MoaiHead.class, "screenNum");
                if (screenNum == 0 && buttonPressed == 0) {
                    int hpToLose = (int) Math.floor(AbstractDungeon.player.maxHealth * (AbstractDungeon.ascensionLevel >= 15 ? 0.18 : 0.125));
                    event.imageEventText.updateBodyText(DESCRIPTIONS[1]);
                    CardCrawlGame.screenShake.shake(ScreenShake.ShakeIntensity.HIGH, ScreenShake.ShakeDur.MED, true);
                    CardCrawlGame.sound.play("BLUNT_HEAVY");
                    AbstractDungeon.player.maxHealth -= hpToLose;
                    if (AbstractDungeon.player.currentHealth > AbstractDungeon.player.maxHealth) {
                        AbstractDungeon.player.currentHealth = AbstractDungeon.player.maxHealth;
                    }

                    if (AbstractDungeon.player.maxHealth < 1) {
                        AbstractDungeon.player.maxHealth = 1;
                    }
                    AbstractDungeon.effectList.add(new ShowCardAndObtainEffect(new Reborn(), (float) Settings.WIDTH / 2.0F - 350.0F * Settings.xScale, (float)Settings.HEIGHT / 2.0F));
                    AbstractImageEvent.logMetricHealAndLoseMaxHP("The Moai Head", "Heal", AbstractDungeon.player.maxHealth, hpToLose);
                    ReflectionHacks.setPrivate(event, MoaiHead.class, "screenNum", 1);
                    event.imageEventText.updateDialogOption(0, OPTIONS[3]);
                    event.imageEventText.clearRemainingOptions();
                    return SpireReturn.Return(null);
                }
            }
            return SpireReturn.Continue();
        }
    }
}
